package jmine.tec.di.injector.impl;

import java.lang.reflect.InvocationTargetException;

import jmine.tec.annotations.Unmodifiable;
import jmine.tec.di.injector.Injector;
import jmine.tec.di.metadata.DependencyMetaData;
import jmine.tec.utils.reflection.fast.FastMethodSingleArgument;

/**
 * Injeta uma dependencia atraves de um metodo. A dependencia a ser injetada vem de um {@link DependencyMetaData}
 * 
 * @author MarceloT
 */
@Unmodifiable
public class MethodBasedInjector extends AbstractMethodBasedInjector implements Injector {

    private final FastMethodSingleArgument method;

    /**
     * C'tor
     * 
     * @param method o metodo a ser invocado
     * @param meta the meta
     */
    public MethodBasedInjector(FastMethodSingleArgument method, DependencyMetaData meta) {
        super(meta);
        this.method = method;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void invokeMethod(Object target, Object argument) throws InvocationTargetException {
        try {
            this.method.invokeSingleArgument(target, argument);
        } catch (RuntimeException e) {
            throw e;
        } catch (Error e) {
            throw e;
        } catch (Exception e) {
            throw new InvocationTargetException(e);
        }
    }

}
